import { uuid } from 'vue-uuid'
import moment from 'moment'

// 修改摄像头唯一标识
export const CHANGE_TREE_NODE_KEY = (state, key) => {
  state.treeNodeKey = key
}

// 修改布局样式类型
export const CHANGE_VIDEO_LAYOUT_TYPE = (state, type) => {
  state.videoLayoutType = type
}

// 修改当前选择项
export const CHANGE_LAYOUT_SELECTED = (state, id) => {
  state.layoutSelected = state.videoLayoutList.find(item => {
    state.isShow = true
    return item.uuid === id
  })
}

// 修改布局数据列表
export const CHANGE_VIDEO_LAYOUT_LIST = (state, list) => {
  state.videoLayoutList = list
}

// 清空某个格子的摄像头信息
export const REMOVE_VIDEO_PLAYER = (state, id) => {
  state.videoLayoutList.forEach(item => {
    if (item.uuid === id) {
      item.cameraInfo[state.treeNodeKey] = ''
    }
  })
}

// 添加列表数据
export const ADD_VIDEO_PLAYER = (state, size) => {
  for (let i = 0; i < size; i++) {
    const _info = {
      uuid: uuid.v1(),
      cameraInfo: {},
      replay: {},
      time: 0
    }
    _info.cameraInfo[state.treeNodeKey] = ''
    state.videoLayoutList.push(_info)
  }
}

// 删除列表部分数据
export const CUT_VIDEO_PLAYER = (state, size) => {
  state.videoLayoutList.splice(state.videoLayoutType.count, size)
  if (state.videoLayoutList.findIndex(item => {
    return item.uuid === state.layoutSelected.uuid
  }) === -1) {
    state.layoutSelected = state.videoLayoutList[state.videoLayoutList.length - 1]
  }
}

// 设置摄像头信息
export const SET_CAMERA_INFO = (state, obj) => {
  state.videoLayoutList.forEach((item, index) => {
    if (item.uuid === obj.layoutId) {
      item.cameraInfo = obj.cameraInfo
      item.replay = obj.replay
      item.time = obj.time
    }
  })
}

// 添加回放视频数据
export const SET_REPLAY_INFO = (state, obj) => {
  state.videoLayoutList.forEach((item, index) => {
    if (item.uuid === obj.layoutId) {
      item.cameraInfo = obj.cameraInfo
      item.replay = formatTimeSlot(obj.cameraInfo.list)
      item.time = 0
    }
  })
}

// 每个窗格对应时间点
export const SET_REPLAY_ITEM_TIME = (state, obj) => {
  state.videoLayoutList.forEach((item, index) => {
    if (item.uuid === obj.uuid) {
      item.time = obj.time
    }
  })
}

// 时间轴当前时间点
export const CHANGE_REPLAY_TIME = (state, time) => {
  state.replayTime = moment().hour(0).minute(time).format('HH:mm')
}

// 是否手动切换时间点
export const CHANGE_IS_MANUAL = (state, flag) => {
  state.isManual = flag
}

// 时间轴当前时间点对应数值
export const CHANGE_REPLAY_POSITION = (state, num) => {
  state.replayPosition = num
}

function formatTimeSlot(list) {
  if (list.length) {
    const _time = []
    let timeSlot = {}
    list.forEach((item, index) => {
      _time.push(timeTransNum(item))
    })
    _time.forEach(itm => {
      timeSlot = Object.assign(timeSlot, itm)
    })
    return timeSlot
  } else {
    return {}
  }
}

function timeTransNum(video) {
  const minutes = {}
  const start = moment(video.StartTime, 'YYYY-MM-DDTHH:mm:ss')
  const end = moment(video.EndTime, 'YYYY-MM-DDTHH:mm:ss')
  const _start = moment(start).startOf('hour')
  for (let i = 0; i < 1440; i++) {
    const c = moment(_start).add(i, 'minute')
    if (c.isBefore(start, 'minute')) {
      continue
    }
    if (c.isAfter(end, 'minute')) {
      break
    }
    minutes[c.format('HH:mm')] = Object.assign({}, video, {
      StartTime: c.format('YYYY-MM-DDTHH:mm:ss')
    })
  }
  return minutes
}

// 修改选择状态
export const CHANGE_IS_REPLACE = (state, flag) => {
  state.isReplace = flag
}

// 修改接口获取的权限id
export const CHANGE_AUTHORIZATION_ID = (state, id) => {
  state.authorizationId = id
}

// 修改默认配置
export const CHANGE_LAYOUT_CONFIG = (state, obj) => {
  state.layoutConfig = obj
}

// 添加摄像头选择列表
export const ADD_CAMERA_LIST = (state, obj) => {
  state.cameraCheckedList.push(obj)
}

// 删除摄像头列表某项
export const CUT_CAMERA_LIST = (state, obj) => {
  let _current = -1
  const current = state.cameraCheckedList.filter((item, index) => {
    if (item[state.treeNodeKey] === obj[state.treeNodeKey]) {
      _current = index
      return item
    }
  })
  if (current.length) {
    state.cameraCheckedList.splice(_current, 1)
  }
}

// 清空摄像头选择
export const CAMERA_CHECKED_CLEAR = state => {
  state.cameraCheckedList = []
  state.cameraPageList = []
  state.cameraPage = 0
  state.cameraTotal = 0
  if (state.carouselSwitch) {
    STOP_CAROUSEL(state)
  }
}

// 选择数据重组
export const CAMERA_PAGES_GROUP = state => {
  const list = state.cameraCheckedList.length ? state.cameraCheckedList : (state.cameraPageList.length ? arrUnTrans(state.cameraPageList) : [])
  if (list.length) {
    state.cameraPageList = arrTrans(state.videoLayoutType.count, list)
    state.cameraTotal = state.cameraPageList.length
    state.cameraPage = 0
    CAMERA_CHANGE_STATE(state)
    STOP_CAROUSEL(state)
  }
}

// 选择数据排列
export const CAMERA_CHANGE_STATE = state => {
  state.videoLayoutList.forEach((item, index) => {
    if (state.cameraPageList.length && state.cameraPageList[state.cameraPage].length > index) {
      item.cameraInfo = state.cameraPageList[state.cameraPage][index]
    } else {
      const info = {}
      info[state.treeNodeKey] = ''
      item.cameraInfo = info
      item.replay = {}
      item.time = 0
    }
  })
}

// 下一页
export const NEXT_CAMERA_PAGE = state => {
  state.cameraPage = state.cameraPage >= state.cameraPageList.length - 1 ? 0 : state.cameraPage + 1
  CAMERA_CHANGE_STATE(state)
}

// 上一页
export const PREV_CAMERA_PAGE = state => {
  state.cameraPage = state.cameraPage <= 0 ? state.cameraPageList.length - 1 : state.cameraPage - 1
  CAMERA_CHANGE_STATE(state)
}

// 轮巡时间
export const CHANGE_CAROUSEL_TIME = (state, time) => {
  state.carouselTime = time
}

// 停止轮巡
export const STOP_CAROUSEL = state => {
  clearInterval(state.carouselTimer)
  state.carouselTimer = null
  state.carouselSwitch = false
}

// 轮巡状态
export const CHANGE_CAROUSEL_SWITCH = (state, flag) => {
  state.carouselSwitch = flag
}

// 轮巡计时器
export const CHANGE_CAROUSEL_TIMER = (state, timer) => {
  state.carouselTimer = timer
}

// 修改云台显示状态
export const CHANGE_IS_PTZ_SHOW = (state, flag) => {
  state.isPtzShow = flag
}

// 一维数组转换为二维数组
function arrTrans(num, arr) {
  const iconsArr = [] // 声明数组
  arr.forEach((item, index) => {
    const page = Math.floor(index / num) // 计算该元素为第几个素组内
    if (!iconsArr[page]) { // 判断是否存在
      iconsArr[page] = []
    }
    iconsArr[page].push(item)
  })
  return iconsArr
}

// 二维数组转换为一维数组
function arrUnTrans(arr) {
  let iconArr = []
  arr.forEach(item => {
    iconArr = [...iconArr, ...item]
  })
  return iconArr
}
